#include "../include/Utils.hh"
#include <random>
#include <chrono>
#include <sstream>
#include <iomanip>

namespace Utils {

std::string generateUniqueAccountId(const std::string& accountTypePrefix, std::set<std::string>& existingIds) {
    // 账号格式：62XX-YYYY-ZZZZ-AAAA
    // XX: 21(储蓄) 或 22(支票)
    // YYYY, ZZZZ, AAAA: 随机生成的数字
    
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> branchCodeDist(1, 9999);
    std::uniform_int_distribution<> accountNumDist(1, 9999);
    
    std::string newAccountId;
    bool isUnique = false;
    
    while (!isUnique) {
        int branchCode = branchCodeDist(gen);
        int accountNum1 = accountNumDist(gen);
        int accountNum2 = accountNumDist(gen);
        
        std::stringstream ss;
        ss << "62" << accountTypePrefix << "-"
           << std::setw(4) << std::setfill('0') << branchCode << "-"
           << std::setw(4) << std::setfill('0') << accountNum1 << "-"
           << std::setw(4) << std::setfill('0') << accountNum2;
        
        newAccountId = ss.str();
        
        // 检查是否唯一
        if (existingIds.find(newAccountId) == existingIds.end()) {
            isUnique = true;
            existingIds.insert(newAccountId); // 添加到已存在ID集合
        }
    }
    
    return newAccountId;
}

std::string getCurrentTimestamp() {
    auto now = std::chrono::system_clock::now();
    auto time = std::chrono::system_clock::to_time_t(now);
    
    std::stringstream ss;
    ss << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S");
    return ss.str();
}

std::string generateTransactionId() {
    static unsigned int counter = 0;
    counter++;
    
    std::stringstream ss;
    ss << "T" << std::setw(4) << std::setfill('0') << counter;
    return ss.str();
}

} // namespace Utils 